using System;
using System.Collections;

namespace Sedna.Core.Controller
{
	public class TypesComparer : IComparer
	{
		private int reverse = 1;

		public TypesComparer()
		{
		}

		public TypesComparer(bool reverse)
		{
			this.reverse = reverse?-1:+1;
			
		}

		public int Compare(object obj1, object obj2)
		{
			if(obj1 == obj2)
			{
				return 0;
			}
			Type type1 = obj1 as Type;
			Type type2 = obj2 as Type;
			if(type1 == null && type2 == null)
			{
				return 0;
			}
			if(type1 == null)//null is less than anything
			{
				return reverse * -1;
			}

			if(type1 == type2)
			{
				return 0;
			}

			return reverse * (type1.IsAssignableFrom(type2)?-1:type2.IsAssignableFrom(type1)?+1:type1.Name.CompareTo(type2.Name));
		}

	}
}
